package com.jshop.controller.admin;
import org.json.JSONException;
import com.jfinal.plugin.activerecord.Page;
import com.jshop.model.Brand;
import com.jshop.utils.DatagridUtil;
/**
 * 后台类 - 商品分类
 * 
 */
public class BrandController extends BaseController {
	public void list(){
		render("list.html");
	}
	public void datagrid(){
		Integer page = getParaToInt("page",1);
		Integer rows = getParaToInt("rows",10);
		String sql="from brand";
		if(getPara("brand_name")!=null&&!getPara("brand_name").equals("")){
			sql+=" where brand_name='"+getPara("brand_name")+"'";
		}
		sql+=" order by brand_order asc";
		Page<Brand> list=Brand.dao.paginate(page, rows, "select *",sql);
		renderJson(DatagridUtil.getList(list));
	}
	public void add() throws JSONException{
		if(isPost()){
			Brand dao=new Brand();
			boolean rs=dao.set("brand_name", getPara("brand_name"))
			.set("brand_logo",getPara("brand_logo"))
			.set("brand_text",getPara("brand_text"))
			.set("brand_word", getPara("brand_word",""))
			.set("brand_order", getParaToInt("brand_order")).save();
			if(rs){
				ajaxJsonSuccessMessage("添加成功");
			}else{
				ajaxJsonErrorMessage("添加失败");
			}
		}else{
			render("add.html");
		}
		
	}
	public void edit() throws JSONException{
		if(isPost()){
			Brand dao=new Brand();
			boolean rs=dao.set("brand_name", getPara("brand_name"))
			.set("brand_id",getPara("brand_id"))
			.set("brand_logo",getPara("brand_logo"))
			.set("brand_text",getPara("brand_text"))
			.set("brand_word", getPara("brand_word",""))
			.set("brand_order", getParaToInt("brand_order")).update();
			if(rs){
				ajaxJsonSuccessMessage("更新成功");
			}else{
				ajaxJsonErrorMessage("更新失败");
			}
		}else{
			setAttr("info", Brand.dao.findById(getParaToInt("id")));
			render("edit.html");
		}
	}
	public void remove() throws JSONException{
		int id=getParaToInt("id");
		boolean rs=Brand.dao.deleteById(id);
		if(rs){
			ajaxJsonSuccessMessage("删除成功");
		}else{
			ajaxJsonErrorMessage("删除失败");
		}
	}
}
